package com.guanyong.files.controllers;


import com.guanyong.files.handles.UploadSaveHandle;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Eric.Zhang on 2017/1/9.
 */
@RestController
@RequestMapping(value="/file")
public class uploadFileApi {


    @Value("${sfl.fileupload.domainn}")
    private String domainn; //"/Images/upload/

    @RequestMapping(value = "uploadfile", method = RequestMethod.POST)
    String uploadfile(
            @RequestParam("uploadEnum") String uploadEnum,
            @RequestParam("uploadKey") String uploadKey,
            @RequestParam(value = "file" ) MultipartFile file,
            HttpServletRequest request
    ) {
        if(uploadKey.equals("7h(sllG.")){
            UploadSaveHandle uploadSaveHandle = new UploadSaveHandle();
            String PicUrl = "";
            if (file!=null && file.getSize()>0){
                try {
                    PicUrl = uploadSaveHandle.SaveFileByMultiFlie(file, request, uploadEnum);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return domainn+PicUrl;
        }



        return "";
    }
    @RequestMapping(value = "test", method = RequestMethod.GET)
    String uploadfile(){
        return "aaaa";
    }

}
